import { BaseModel, sequelize, Sequelize } from "./basemodel.js";

class Courses extends BaseModel {
	static init() {
		return super.init(
			{
				
				course_id: { type: Sequelize.INTEGER, primaryKey: true, defaultValue: Sequelize.literal('DEFAULT') },
				course_name: { type:Sequelize.STRING , allowNull: false ,defaultValue: Sequelize.literal('DEFAULT') },
				description: { type:Sequelize.STRING  ,defaultValue: Sequelize.literal('DEFAULT') },
				credit_hours: { type:Sequelize.NUMBER  ,defaultValue: Sequelize.literal('DEFAULT') },
				daily_weight: { type:Sequelize.NUMBER , allowNull: false  },
				exam_weight: { type:Sequelize.NUMBER , allowNull: false  },
				homework_weight: { type:Sequelize.NUMBER , allowNull: false  },
				start_date: { type:Sequelize.DATEONLY  ,defaultValue: Sequelize.literal('DEFAULT') },
				end_date: { type:Sequelize.DATEONLY  ,defaultValue: Sequelize.literal('DEFAULT') },
				teacher_id: { type:Sequelize.INTEGER  ,defaultValue: Sequelize.literal('DEFAULT') },
				status: { type:Sequelize.STRING  ,defaultValue: Sequelize.literal('DEFAULT') },
				created_at: { type:Sequelize.DATE , allowNull: false  },
				updated_at: { type:Sequelize.DATE , allowNull: false  },
				course_number: { type:Sequelize.STRING , allowNull: false ,defaultValue: Sequelize.literal('DEFAULT') }
			}, 
			{ 
				sequelize,
				
				tableName: "courses",
				modelName: "courses",
			}
		);
	}
	
	static listFields() {
		return [
			'course_id', 
			'course_name', 
			'description', 
			'credit_hours', 
			'daily_weight', 
			'exam_weight', 
			'homework_weight', 
			'start_date', 
			'end_date', 
			'teacher_id', 
			'status', 
			'created_at', 
			'updated_at', 
			'course_number'
		];
	}

	static viewFields() {
		return [
			'course_id', 
			'course_name', 
			'description', 
			'credit_hours', 
			'daily_weight', 
			'exam_weight', 
			'homework_weight', 
			'start_date', 
			'end_date', 
			'teacher_id', 
			'status', 
			'created_at', 
			'updated_at', 
			'course_number'
		];
	}

	static editFields() {
		return [
			'course_id', 
			'course_name', 
			'description', 
			'credit_hours', 
			'daily_weight', 
			'exam_weight', 
			'homework_weight', 
			'start_date', 
			'end_date', 
			'teacher_id', 
			'status', 
			'course_number'
		];
	}

	
	static searchFields(){
		return [
			Sequelize.literal("course_id LIKE :search"), 
			Sequelize.literal("course_name LIKE :search"), 
			Sequelize.literal("description LIKE :search"), 
			Sequelize.literal("status LIKE :search"), 
			Sequelize.literal("course_number LIKE :search"),
		];
	}

	static async upsert(values, options) {
		const where = options.where;
		const existingRecord = await this.findOne({ where });
		
		if (existingRecord) {
			return await this.update(values, { where });
		} else {
			return await this.create(values);
		}
	}
}
Courses.init();
export default Courses;
